#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <climits>
#include <vector>
#define int long long
#define x first 
#define y second 
using namespace std;
const int N = 1e5 + 10;
bool vis[N];//判断结点有没有被访问
bool col[N];//染色
int a[N];//存储权值
vector<int> G[N];//构造树
int res;

bool is_prime(int x)
{
    if(x < 2) return false;
    for(int i = 2; i <= x / i; i++)
    {
        if(x % i == 0) return false;
    }
    return true;
}

int dfs(int rt)
{
    vis[rt] = true;
    for(int i = 0; i < G[rt].size(); i++)
    {
        int son = G[rt][i];
        // cout << son << endl;
        if(vis[son] == true) continue;
        dfs(son);
        if(is_prime(a[son] + a[rt])) 
        {
            if(col[rt] == 0) 
            {
               col[son] = 1;
              // cout << rt << " " << son << endl;
               res++;
            }
        }
    }
    return res;
}

void solve()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n - 1; i++)//注意不能读n个
    {
        int u,v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
   cout << dfs(1) << endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int t = 1;
    while(t -- ) solve();
    return 0;
} 
 